const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function setStorageSync(key, data, callback) {
  wx.setStorage({
    key,
    data,
    success: (res) => {
      callback && callback;
    }
  })
}

function route(url, data = {}, method = 'navto') {
  url = url + '?data' + '=' + JSON.stringify(data)
  switch (method) {
    case 'navto':
      wx.navigateTo({ url })
      break;
    case 'redto':
      wx.redirectTo({ url })
      break;
    case 'rel':
      wx.reLaunch({ url })
      break;
    case 'switchtab':
      wx.switchTab({ url })
      break;
  }
}

module.exports = {
  formatTime: formatTime,
  setStorageSync,
  route
}